<template>
  <view class="apply_bank_qrcode_step4 need_scroll_top_view">
    <steps :active="4"></steps>
    <view class="cut-line"></view>
    <scroll-view class="scroll-wrap" :scroll-y="true" :style="{height: (safeHeight - safeWidth / 375 * (101 + 64 + 11)) + 'px'}">
      <view class="form-wrap">
        <form>
          <view class="form-item self-border-bottom">
            <text class="form-item-label">账户名称</text>
            <input type="text" v-model="data.settlementAccountName" :disabled="isDisabled" class="form-item-input" placeholder="请填写您的银行账户名称 例“李某某”或“某某公司”" placeholder-class="input-placeholder"/>
            <view class="no-arrow"></view>
          </view>
          <view class="form-item self-border-bottom">
            <text class="form-item-label">银行账号</text>
            <input type="number" v-model="data.bankCard" :disabled="isDisabled" class="form-item-input" placeholder="请填写您的银行账户" placeholder-class="input-placeholder"/>
            <view class="no-arrow"></view>
          </view>
          <view class="form-item self-border-bottom">
            <text class="form-item-label">开户银行</text>
            <picker :value="bankIndex" :range="bankArray" :disabled="isDisabled" @change="bindBankPickerChange" class="form-item-pick" range-key="name">
              <view class="pick-value-select" v-if="bankArray && bankArray.length > 0 && selectBank">{{bankArray[bankIndex].name}}</view>
              <view class="pick-value" v-else>请选择开户银行</view>
            </picker>
            <view class="arrow"></view>
          </view>
      
          <view class="form-item self-border-bottom">
            <text class="form-item-label">开户地区</text>
            <picker :value="multiDistrictIndex" range-key="name" :disabled="isDisabled" :range="multiDistrictArray" @cancel="bindDistrictPickerCancel" @change="bindDistrictPickerChange" @columnchange="bindDistrictColumnPickerChange" class="form-item-pick" mode="multiSelector">
              <view class="pick-value-select" v-if="multiDistrictArray && multiDistrictArray.length > 0 && selectDistrict">
                <text v-if="multiDistrictArray[0] && multiDistrictArray[0][multiDistrictIndex[0]]">{{multiDistrictArray[0][multiDistrictIndex[0]].name}}</text>
                <text v-if="multiDistrictArray[1] && multiDistrictArray[1][multiDistrictIndex[1]]">{{multiDistrictArray[1][multiDistrictIndex[1]].name}}</text>
                <text v-if="multiDistrictArray[2] && multiDistrictArray[2][multiDistrictIndex[2]]">{{multiDistrictArray[2][multiDistrictIndex[2]].name}}</text>
              </view>
              <view class="pick-value" v-else>请选择您的开户地区</view>
            </picker>
            <view class="arrow"></view>
          </view>
          <view class="form-item self-border-bottom">
            <text class="form-item-label">开户支行</text>
            <input type="text" v-model="data.branchBankName" :disabled="isDisabled" class="form-item-input" placeholder="请填写您的开户支行" placeholder-class="input-placeholder"/>
            <view class="no-arrow"></view>
          </view>
          <view class="form-item">
            <text class="form-item-label">结算税率</text>
            <!-- <input type="number" v-model="data.rate" class="form-item-input" placeholder="请填写您的开户行" placeholder-class="input-placeholder"/> -->
            <view class="rate-info">
              收款码收款流水扣除税率10%
            </view>
            <view class="no-arrow"></view>
          </view>
          <view class="cut-line"></view>
          <view class="form-other-item">
            <view class="form-item-head">
              <view class="title">
                结算账户照片
                <text class="tip">（银行开户证明或银行卡正面照片）</text>
              </view>
              <view class="sample" @click="previewImg(1)">
                <text>示例</text>
              </view>
            </view>
            <view class="form-item-content" style="display: flex;">
              <view class="common-img-wrap" @click="uploadFile(1)">
                <view class="no-upload-img" v-if="!data.bankFileId || !data.bankFileIdUrl">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传银行卡正面
                  </view>
                </view>
                <view class="upload-img-wrap" v-else>
                  <image :src="data.bankFileIdUrl" mode="" class="upload-img"></image>
                </view>
              </view>
              <!-- <view class="common-img-wrap" style="margin-left: 27rpx;" @click="uploadFile(2)">
                <view class="no-upload-img" v-if="!data.bankCardBackFileUrl || !data.bankCardBackFileId">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传银行卡反面
                  </view>
                </view>
                <view class="upload-img-wrap" v-else>
                  <image :src="data.bankCardBackFileUrl" mode="" class="upload-img"></image>
                </view>
              </view> -->
            </view>
          </view>
          <view class="attention">
            <view class="">
              <text class="red-start">*</text>
              <text>1.企业类型商户，上传开户证明或银卡基本开户证明等银行机构开具的对公账户证明。</text>
            </view>
            <view class="">
              <text>2.个体工商户类型的商户，提供法人名下的银行卡照片。</text>
            </view>
          </view>
        </form>
      </view>
    </scroll-view>
    
    <view class="qrcode_selected_platform-footer">
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="submitInfo" v-if="!status || status == 3">提交申请</button>
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="nextStep" v-else>查看收款码</button>
    </view>
  </view>
</template>

<script>
import steps from '../common/bankSteps.vue'
import formValid from '@/unit/graceChecker.js'  
import { multiDistrict, uploadFile, checkImage } from '@/unit/common.js'
import { getUserBankListSyncApi, saveBankInfoApi, getSubmitBankInfoApi, getStepStatusApi, submitBankAllApplyInfoApi } from '@/service/qrCode/qrCode.service.js'
import { scrollToTargetPosition, setSafeAreaSize } from '@/mixin/index.mixin.js'
export default {
  mixins: [scrollToTargetPosition, setSafeAreaSize],
  components: {
    steps
  },
  computed: {
    isDisabled: function () {
      return this.status == 1 || this.status == 2;
    }
  },
  data () {
    return {
      status: null,
      bankArray: [], // 银行列表
      bankIndex: 0,
      selectBank: false,
      selectDistrict: false,
      multiDistrictIndex: [], // 省市区选择的index
      multiDistrictArray: [], // 省市区数据 Array[Object]
      multiDistrictIds: [], // 省市区ID
      originMultiDistrictIndex: [], // 原始数据 用于取消选择省市区，还原原始数据
      originMultiDistrictArray: [], // 原始数据 用于取消选择省市区，还原原始数据
      originMultiDistrictIds: [], // 原始数据 用于取消选择省市区，还原原始数据
      data: {
        storeCollectId: null,
        settlementAccountName: '', // 账户名称
        bankCard: '', // 银行账号
        bankId: null, // 开户银行
        bankName: null, // 开户银行名称
        districtId: null, // 开户地区
        // 结算税率
        branchBankName: '', // 开户支行
        bankFileId: null,
        bankFileIdUrl: null, // 银行卡正面
      }
    }
  },
  methods: {
    previewImg (type) { // 查看示例图片
      switch (type) {
      case 1:
        checkImage([this.$store.state.imgBaseUrl + '/qrcode/sample/settlement_account.png'], 0);
        break;
      default:
      }
    },
    async getStepStatus () {
      let data = await getUserBankListSyncApi({
        size: 10000,
        current: 1
      }); // 获取银行列表数据
      this.bankArray = data.records;
      getStepStatusApi({}, res => {
        if (res.status == 0) {
          this.status = res.data.status;
          this.data.storeCollectId = res.data.id;
          if (res.data.bankStatus == 1) {
            this.getInfo(res.data.id);
          } else {
            this.init();
          }
        }
      });
    },
    getInfo (id) {
      getSubmitBankInfoApi({id: id}, res => {
        if (res.status == 0) {
          this.data.settlementAccountName = res.data.settlementAccountName; // 账户名称
          this.data.bankCard = res.data.bankCard; // 银行账号
          if (res.data.bankId) {
            this.data.bankId = res.data.bankId; // 开户银行
            this.bankArray.forEach((item, index) => {
              if (item.id == res.data.bankId) {
                this.bankIndex = index;
                this.selectBank = true;
              }
            });
          }
          
          this.data.bankName = res.data.bankName; // 开户银行名称
          this.data.districtId = res.data.districtId; // 开户地区
          if (res.data.districtIds && res.data.districtIds.length > 0) {
            this.initDistrictData(res.data.districtIds);
            this.selectDistrict = true;
          } else {
            this.initDistrictData();
          }
          // 结算税率
          this.data.branchBankName = res.data.branchBankName; // 开户支行
          this.data.bankFileId = res.data.bankFile.id;
          this.data.bankFileIdUrl = res.data.bankFile.url; // 银行卡正面
        }
      }, null, {showErrorMsg: true});
    },
    uploadFile (type) {
      if (this.status == 1 || this.status == 2) {
        return false;
      }
      uploadFile({
        success: (res) => {
          switch (type) {
          case 1:
            this.data.bankFileId = res.data.id;
            this.data.bankFileIdUrl = res.data.url;
            break;
          // case 2:
          //   this.data.bankCardBackFileId = res.data.id;
          //   this.data.bankCardBackFileUrl = res.data.url;
          //   break;
          default:
          }
        }
      });
    },
    async initDistrictData (district) { // 初始化省市区数据
      let {multiDistrictArray, multiDistrictIndex, multiDistrictIds} = await multiDistrict.initMultDistrictArray({
        district: district
      });
      this.multiDistrictArray = multiDistrictArray;
      this.multiDistrictIndex = multiDistrictIndex;
      this.multiDistrictIds = multiDistrictIds;
      this.originMultiDistrictArray = JSON.parse(JSON.stringify(multiDistrictArray));
      this.originMultiDistrictIndex = JSON.parse(JSON.stringify(multiDistrictIndex));
      this.originMultiDistrictIds = JSON.parse(JSON.stringify(multiDistrictIds));
    },
    bindDistrictPickerCancel (e) { // 取消省市区的选择
      this.multiDistrictArray = JSON.parse(JSON.stringify(this.originMultiDistrictArray));
      this.multiDistrictIndex = JSON.parse(JSON.stringify(this.originMultiDistrictIndex));
      this.multiDistrictIds = JSON.parse(JSON.stringify(this.originMultiDistrictIds));
    },
    bindDistrictPickerChange (e) { // 确认省市区的选择
      this.selectDistrict = true;
      this.originMultiDistrictArray = JSON.parse(JSON.stringify(this.multiDistrictArray));      
      this.originMultiDistrictIndex = JSON.parse(JSON.stringify(this.multiDistrictIndex));
      this.originMultiDistrictIds = JSON.parse(JSON.stringify(this.multiDistrictIds));
      this.data.districtId = this.multiDistrictIds[this.multiDistrictIds.length - 1];
    },
    async bindDistrictColumnPickerChange (e) { // 省市区的列发生变化
      if (e.detail.column < this.multiDistrictArray.length - 1) {
        let {multiDistrictArray, multiDistrictIndex, multiDistrictIds} = await multiDistrict.bindDistrictColumnPickerChange({
          column: e.detail.column,
          index: e.detail.value,
          multiDistrictIndex: JSON.parse(JSON.stringify(this.multiDistrictIndex)),
          multiDistrictArray: JSON.parse(JSON.stringify(this.multiDistrictArray))
        });
        this.multiDistrictArray = multiDistrictArray;
        this.multiDistrictIndex = multiDistrictIndex;
        this.multiDistrictIds = multiDistrictIds;
      } else {
        this.multiDistrictIndex.splice(e.detail.column, 1, e.detail.value);
        this.multiDistrictIds.splice(e.detail.column, 1, this.multiDistrictArray[e.detail.column][e.detail.value].id);
      }
    },
    bindBankPickerChange (e) {
      this.selectBank = true;
      this.bankIndex = e.target.value;
      this.data.bankId = this.bankArray[e.target.value].id;
      this.data.bankName = this.bankArray[e.target.value].name;
    },
    nextStep () {
      uni.redirectTo({
        url: '/pages/qrCode/index?qrcodeType=1'
      });
    },
    submitInfo () {
      let rule = [
        {name:'settlementAccountName', checkType : 'notnull', checkRule:'',  errorMsg:'请填写账户名称'},
        {name:'bankCard', checkType : 'notnull', checkRule:'',  errorMsg:'请填写银行账号'},
        {name:'bankId', checkType : 'notnull', checkRule:'',  errorMsg:'请选择开户银行'},
        {name:'districtId', checkType : 'notnull', checkRule:'',  errorMsg:'请选择开户地区'},
        {name:'branchBankName', checkType : 'notnull', checkRule:'',  errorMsg:'请填写开户支行'},
        {name:'bankFileIdUrl', checkType : 'notnull', checkRule:'',  errorMsg:'请上传银行卡正面照'},
      ];
      var checkRes = formValid.check(this.data, rule);
      if (checkRes) {
        saveBankInfoApi(this.data, res => {
          if (res.status == 0) {
            uni.showToast({
              title: '提交结算信息成功！',
              success: () => { 
                this.submitBankAllApplyInfo();
              }
            })
          }
        }, null, {showErrorMsg: true});
      } else {
        uni.showToast({ title: formValid.error, icon: "none" });
      }
    },
    submitBankAllApplyInfo () {
      submitBankAllApplyInfoApi({
        id: this.data.storeCollectId
      }, res => {
        uni.removeStorageSync('yLeasypayKey');
        uni.removeStorageSync('yLfirstTrialKey');
        if (res.status == 0) {
          this.status = 1;
          // uni.redirectTo({
          //   url: '/pages/qrCode/result?type=1&status=1'
          // })
        }
      }, null, {showErrorMsg: true});
    },
    async init (option) {
      this.initDistrictData();
      this.data.bankId = this.bankArray[this.bankIndex].id;
    }
  },
  mounted () {
    this.getStepStatus();
  },
  onShow () {
    // 
  },
  onLoad (option) {
    // this.init(option);
  }
}
</script>

<style lang="scss" scoped>
  .apply_bank_qrcode_step4 {
    position: relative;
    min-height: calc(100vh);
    background-color: #FFFFFF;
    .cut-line {
      height: 22rpx;
      background-color: #FAFAFA;
    }
    .scroll-wrap {
      height: calc(100vh - 62px - 101px);
    }
    .form-wrap {
      // margin-top: 20rpx;
      background: #FFFFFF;
      .rate-info {
        flex: 1;
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #666666;
      }
      .self-border-bottom {
        border-bottom: 1rpx solid #E1E1E1;
      }
      .self-margin-top {
        margin-top: 22rpx;
      }
      
      .pick-value-select {
        color: #333333;
        font-size: 28rpx;
      }
      
      .pick-value {
        color: #999999;
        font-size: 24rpx;
      }
      .form-item {
        display: flex;
        width: 718rpx;
        justify-content: center;
        align-items: center;
        margin-left: 32rpx;
        // height: 100rpx;
        padding: 36rpx 0;
        
        .form-item-label {
          width: 140rpx;
          text-align: left;
          padding: 0 32rpx 0 0;
          font-size: 28rpx;
          color: #333333;
          font-weight:500;
        }
        .form-item-input {
          font-size: 28rpx;
          height: 38rpx;
          color: #333333;
          flex: 1;
          .input-placeholder {
            color: #999999;
          }
        }
        .form-item-pick {
          flex: 1;
        }
        .arrow {
          margin-right: 33rpx;
          border-right: 3rpx solid #8A8A8A;
          border-top: 3rpx solid #8A8A8A;
          height: 18rpx;
          width: 18rpx;
          transform: rotate(45deg);
          -webkit-transform: rotate(45deg);
          /*不加这两个属性三角会比上一个略丑, 大家可以试一下*/
          border-left: 2rpx solid transparent;
          border-bottom: 2rpx solid transparent;
        }
        .no-arrow {
          margin-left: 64rpx;
        } 
      }
      
      .form-other-item {
        padding: 0 34rpx 22rpx;
        .form-item-head {
          overflow: hidden;
          padding-top: 40rpx;
          display: flex;
          justify-content: space-between;
          .title {
            // float: left;
            color: #333333;
            font-size: 36rpx;
          }
          .sample {
            width: 80rpx;
            // float: right;
            text {
              color: #FF7473;
              font-size: 28rpx;
              text-decoration: underline;
            }
          }
          &:after {
            clear: both;
          }
        }
        .form-item-content {
          margin-top: 30rpx;
          .common-img-wrap {
            width: 330rpx;
            height: 200rpx;
            border: 1px dashed #E1E1E1;
            border-radius: 16rpx;
            
            .no-upload-img {
              width: 100%;
              height: 100%;
              display: flex;
              flex-flow: column;
              align-items: center;
              justify-content: center;
              .img {
                width: 80rpx;
                height: 80rpx;
              }
              .text {
                margin-top: 29rpx;
                color: #666666;
                font-size: 24rpx;
                font-weight:500;
              }
            }
            
            .upload-img-wrap {
              width: 100%;
              height: 100%;
              
              .upload-img {
                width: 100%;
                height: 100%;
                border-radius: 16rpx;
              }
            }
          }
          .upload-img-wrap {
            width: 100%;
            height: 100%;
            
            .upload-img {
              width: 100%;
              height: 100%;
              border-radius: 16rpx;
            }
          }
        }
        .tip {
          font-size: 22rpx;
          font-family:PingFang SC;
          color: #999999;
          margin-left: 10rpx;
        }
      }
    }
    
    .attention {
      margin: 38rpx 32rpx;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
      line-height: 40rpx;
      .red-start {
        font-size: 28rpx;
        color: #FF7374;
      }
    }
    
    .qrcode_selected_platform-footer {
      position: fixed;
      bottom: 0rpx;
      left: 0rpx;
      width: 100%;
      height: 125rpx;
      text-align: center;
      display: flex;
      align-items: center;
      justify-content: center;
      border-top: 1rpx solid #E1E1E1;
      background: #FFFFFF;
      .qrcode_selected_platform-footer-btn {
        width: 687rpx;
        height: 90rpx;
        line-height: 90rpx;
        background-color: #434343;
        color: #F6DA99;
        font-size: 30rpx;
      }
    }
  }
</style>
